﻿using System;
using System.Collections.Generic;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Transfer;

namespace xstore.service
{
    public class S3ClientToCtyun
    {

        private const string AK = "";

        private const string SK = "";

        private const string ENDPOINT = "https://nxoss.xstore.ctyun.cn";

        private readonly AmazonS3Client s3Client;

        private readonly TransferUtility transferUtility;

        private static S3ClientToCtyun instance;

        /**
         * 单例
         */
        public static S3ClientToCtyun GetInstance()
        {
            if (instance == null)
            {
                instance = new S3ClientToCtyun();
            }
            return instance;
        }

        public S3ClientToCtyun()
        {
            var credentials = new BasicAWSCredentials(AK, SK);

            var conf = new AmazonS3Config
            {
                ServiceURL = ENDPOINT

            };

            this.s3Client = new AmazonS3Client(credentials, conf);
            this.transferUtility = new TransferUtility(this.s3Client);
        }

        /**
         * 取得该用户下所有 Bucket
         */
        public List<String> ListAllBuckets()
        {
            var task = this.s3Client.ListBucketsAsync();
            var res = task.Result;
            if (res.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception();
            }
            var bucketNames = new List<string>();
            res.Buckets.ForEach(b =>
            {
                bucketNames.Add(b.BucketName);
            });
            return bucketNames;
        }

        /**
         * 取得指定 Bucket 的所有对象的 Key
         */
        public List<string> ListAllObjectKeysInBucket(String bucket)
        {
            var task = this.s3Client.ListObjectsAsync(bucket);
            var res = task.Result;
            var list = new List<string>();
            res.S3Objects.ForEach(s3Obj =>
            {
                list.Add(s3Obj.Key);
            });
            return list;
        }

        /**
         * 创建一个 bucket
         */
        public string CreateBucket(String bucket)
        {
            var task = this.s3Client.PutBucketAsync(bucket);
            var res = task.Result;
            return res.HttpStatusCode.ToString();
        }

        /**
         * 删除一个 bucket
         */
        public string DeleteBucket(string bucket)
        {
            var task = this.s3Client.DeleteBucketAsync(bucket);
            var res = task.Result;
            return res.HttpStatusCode.ToString();
        }

        /**
         *  分片上传
         **/
        public void MultipartUpload(string bucket, string key, string filePath)
        {
            var request = new TransferUtilityUploadRequest
            {
                BucketName = bucket,
                Key = key,
                FilePath = filePath
            };
            request.UploadProgressEvent += UploadEventHandler;
            this.transferUtility.Upload(request);
        }

        /**
         * 输出进度
         */
        private static void UploadEventHandler(object sender, UploadProgressArgs args)
        {
            Console.Out.WriteLine("Uploading.. {0}/{1}", args.TransferredBytes, args.TotalBytes);
        }

    }
}
